//
//  NXFeedbackViewController.m
//  wallet
//
//  Created by 刘威 on 2017/7/6.
//  Copyright © 2017年 刘威. All rights reserved.
//

#import "NXFeedbackViewController.h"
#import <IQTextView.h>


@interface NXFeedbackViewController () <UITableViewDelegate,UITableViewDataSource,UITextViewDelegate> {
    IQTextView *_textView;
    UILabel    *_countLabel;
}

@property (strong, nonatomic) UITableView *tableView;


@end

@implementation NXFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"意见反馈";
    [self createNavigationBackItem];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor grayBackgroundColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(self.view);
    }];

    // Do any additional setup after loading the view.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (void)actionBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"c"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"c"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        _textView = [[IQTextView alloc] init];
        _textView.font = [UIFont contentFont];
        _textView.textColor = [UIColor contentColor];
        _textView.placeholder = @"请输入您宝贵的意见...";
        _textView.delegate = self;
        [cell addSubview:_textView];
        [_textView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.mas_left).offset((20));
            make.right.equalTo(cell.mas_right).offset(-(20));
            make.top.equalTo(cell.mas_top).offset((15));
            make.bottom.equalTo(cell.mas_bottom).offset(-(35));
        }];
        
        
        UILabel *label = [[UILabel alloc] init];
        label.textColor = [UIColor grayContentTitleColor];
        label.font = [UIFont tabBarTitleFont];
        label.textAlignment = NSTextAlignmentRight;
        [cell addSubview:label];
        [label makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(cell.mas_right).offset(-(20));
            make.bottom.equalTo(cell.bottom).offset(-(10));
        }];
        label.text = @"300";
        
        UILabel *countLabel = [[UILabel alloc] init];
        countLabel.textColor = [UIColor grayContentTitleColor];
        countLabel.font = [UIFont contentFont];
        countLabel.textAlignment = NSTextAlignmentRight;
        [cell addSubview:countLabel];
        [countLabel makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(label.left);
            make.bottom.equalTo(label.bottom);
        }];
        countLabel.text = @"300/";
        _countLabel = countLabel;
        
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (150);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 140;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    [button setBackgroundColor:[UIColor redButtonColor]];
    [button setTitle:@"提交"];
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 3;
    [button setTitleColor:[UIColor whiteColor]];
    button.titleLabel.font = [UIFont titleFont];
    [view addSubview:button];
    [button makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(view);
        make.height.equalTo(@((45)));
        make.width.equalTo(@(SCREEN_WIDTH - (40)));
    }];
    [button addTarget:self action:@selector(submitClick:)];
    return view;
}

- (void)submitClick:(id)sender {
    if (!_textView.text.length) {
        [SVProgressHUD showInfoWithStatus:@"请输入您的意见"];
        return;
    }
    [SVProgressHUD show];
    [_textView resignFirstResponder];
    [self performSelector:@selector(hhh) withObject:nil afterDelay:0.8];
}

- (void)hhh {
    [SVProgressHUD showSuccessWithStatus:@"提交成功"];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)textViewDidChange:(UITextView *)textView {
    NSString *str = textView.text;
    NSInteger count = 300 - str.length;
    if (count <= 0) {
        count = 0;
    }
    _countLabel.text = [NSString stringWithFormat:@"%ld/",(long)count];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSString *str = textView.text;
    if (str.length > 299) {
        if ([text isEqualToString:@""]) {
            return YES;
        }
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
